﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using wms.Common;

namespace wms.Pages
{
    /// <summary>
    /// Pagination.xaml 的交互逻辑
    /// </summary>
    public partial class Pagination : UserControl
    {
        //创建一个委托
        public delegate void ClickEvent();

        public event ClickEvent Go;

        private int _pageNo = 1;
        private int _pagePer = 100;
        private int _pages = 1;
        private int _count = 0;

        public Pagination()
        {

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                ChangeLanguage.changePage(Variable.LanguageSeesion.ToString());
            }
            InitializeComponent();
        }

        private void ReMessage()
        {
            if (this._pages <= 1)
            {
                this.lbl_page.Content = String.Format("共{0}条", this._count);
                this.btn_pre.Visibility = Visibility.Collapsed;
                this.btn_next.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.lbl_page.Content = String.Format("共{0}条 每页{1}条，当前{2}/{3}", this._count, this._pagePer, this._pageNo, this._pages);
                this.btn_pre.Visibility = Visibility.Visible;
                this.btn_next.Visibility = Visibility.Visible;
            }
        }

        public void Reset()
        {
            this._pageNo = 1;
        }

        public void Init(int count, int? pagePer = null)
        {
            this._count = count;
            if (pagePer != null)
            {
                this._pagePer = (int)pagePer;
            }
            this._pages = (count + this._pagePer - 1) / this._pagePer;
            ReMessage();
        }
        public int PagePer
        {
            get
            {
                return this._pagePer;
            }
            set
            {
                this._pagePer = value;
            }
        }

        public int Offset
        {
            get
            {
                return (this._pageNo - 1) * this._pagePer;
            }
        }

        private void btn_pre_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (this._pageNo <= 1)
            {
                return;
            }
            else
            {
                this._pageNo--;
            }
            ReMessage();
            if (this.Go != null)
            {
                Go.Invoke();
            }
        }

        private void btn_next_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (this._pageNo >= this._pages)
            {
                return;
            }
            else
            {
                this._pageNo++;
            }
            ReMessage();
            if (this.Go != null)
            {
                Go.Invoke();
            }
        }
    }
}
